package practice.annotaion.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import practice.annotaion.spring.door.Config;
import practice.annotaion.spring.entrance.HomeController;
import practice.annotaion.spring.entrance.InfraredRaySensor;

public class DoorAcquisition {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context = makeContextByClass();
		run(context);
		context = makeContextByClass();
		run(context);
	}

	// @Configuration 어노테이션이 적용된 클래스를 설정 정보 이용
	private static ApplicationContext makeContextByClass() {
		AbstractApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
		context.registerShutdownHook();
		return context;
	}

	private static void run(ApplicationContext context) {
		HomeController homeControl = context.getBean("homeController", HomeController.class);
		homeControl.checkSensorAndAlarm();
		InfraredRaySensor doorSensor = context.getBean("doorSensor", InfraredRaySensor.class);
		doorSensor.foundObject();
		homeControl.checkSensorAndAlarm();
	}

	private static ApplicationContext makeContextByXml() {
		String[] configLocations = new String[] { "practice/annotaion/spring/Door.xml" };
		AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
		context.registerShutdownHook();
		return context;
	}
}
